package com.fieldchain.ex.fa100.ui.money.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/13
 */
public class MarketChoseAdapter extends RecyclerView.Adapter<MarketChoseAdapter.ViewHolder> {
    private Context mContext;

    public MarketChoseAdapter(Context mContext) {
        this.mContext = mContext;
    }

    private List<String> list = new ArrayList<>();

    public void setData(List<String> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_address, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String adr = list.get(position);
        holder.tvMarket.setText(adr.toUpperCase());
        if (position == 0) {
            holder.cb.setChecked(true);
        }
        holder.reaItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("market", list.get(position));
                ((Activity) mContext).setResult(100, intent);
                ((Activity) mContext).finish();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout reaItem;
        private TextView tvMarket;
        private CheckBox cb;

        public ViewHolder(View itemView) {
            super(itemView);
            reaItem = itemView.findViewById(R.id.rea_item);
            tvMarket = itemView.findViewById(R.id.tv_market);
            cb = itemView.findViewById(R.id.cb);
        }
    }
}
